package tt.dz.egobus.servicemanager.controller.alert;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tt.dz.egobus.servicemanager.base.controller.EntityController;
import tt.dz.egobus.servicemanager.base.service.ExtendBaseService;
import tt.dz.egobus.servicemanager.core.exception.BusinessException;
import tt.dz.egobus.servicemanager.entities.BusinessExceptionAlert;
import tt.dz.egobus.servicemanager.service.BusService;
import tt.dz.egobus.servicemanager.service.BusinessExceptionAlertService;
import tt.dz.egobus.servicemanager.vo.BusinessExceptionAlertViewVo;

import java.util.HashMap;
import java.util.Map;

import static tt.dz.egobus.servicemanager.bean.ServiceManagerCodeStatus.ALERT_CANCEL_NOT_CHANGE;

/**
 * @author Anson
 * @date 17-4-17
 * @name
 * @since 1.0.4
 */
@RestController
@RequestMapping("/exceptionAlert")
public class BusinessExceptionAlertCntroller extends EntityController<BusinessExceptionAlert, BusinessExceptionAlertViewVo, String> {

    @Autowired
    @Qualifier("businessExceptionAlertServiceImpl")
    private BusinessExceptionAlertService service;

    @Autowired
    @Qualifier("busServiceImpl")
    private BusService busService;

    @Override
    protected ExtendBaseService<BusinessExceptionAlert, String> getSelfService() {
        return service;
    }

    @Override
    protected BusinessExceptionAlertViewVo newViewVo() {
        return new BusinessExceptionAlertViewVo();
    }


    @Override
    protected void preUpdateHandler(BusinessExceptionAlert alert) throws BusinessException {
        if (!alert.assertCancel()) {
            //当前存在取消的时候
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("eq#id", alert.getId());
            params.put("eq#status", BusinessExceptionAlert.ExceptionAlertStatus.CANCEL.getCode());
            int count = this.service.count(params);
            if (count > 0) {
                throw new BusinessException(ALERT_CANCEL_NOT_CHANGE.getCodeStatus());
            }
        }
    }

    /**
     * @param alert 当前上报的异常
     * @author Anson
     * @date 17-5-15
     * @name 修改相关状态后, 针对与车辆异常的处理, 需要同步车辆状态
     * @since 1.0.4
     */
    @Override
    protected void postUpdateHandler(BusinessExceptionAlert alert) throws BusinessException {
        //修改相关状态后,针对与车辆异常的处理,需要同步车辆状态
        this.modifyDealExceptionOverBusStatus(alert);
    }

    /**
     * @param alert 当前车辆 状态
     * @author Anson
     * @date 17-5-15
     * @name 异常状态为车辆上报的时候, 根据当前状态修改状态
     * 当前异常状态为处理完毕{@linkplain tt.dz.egobus.servicemanager.entities.BusinessExceptionAlert.ExceptionAlertStatus#FINISH}
     * 取消{@linkplain tt.dz.egobus.servicemanager.entities.BusinessExceptionAlert.ExceptionAlertStatus#CANCEL}
     * 才进行当前状态修改
     * @since 1.0.4
     */
    private void modifyDealExceptionOverBusStatus(BusinessExceptionAlert alert) {
        String busNo = null;
        // 确信当前是车辆异常上报
        if (alert != null && alert.assertBus()
                // 确信当前是否存于异常处理完毕以及取消状态的时候
                && (alert.assertCancel() || alert.assertFinish())
                //确信当前车牌号是否存在的时候
                && !StringUtils.isBlank(busNo = alert.getBusNo())
                //还是存在当前车辆的其他异常状态
                && !this.service.isExistValidateBusUnDeal(busNo)
                ) {
            busService.updateExceptionBusUnUsed(busNo);
        }
    }

}
